package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
// 静态sql 没有拼接的
public class JdbcDemo6
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("name = " );
        String name = scanner.nextLine();
        System.out.println("password = " );
        String password = scanner.nextLine();
        // 预编译sql
        try(Connection connection = DBUtil.getConnection())
        {
            String sql = "select username,password,nickname from userinfo1 where username = ? and password = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,password);
            ResultSet setResult = preparedStatement.executeQuery();
            if (setResult.next())
            {
                System.out.println("name = " + setResult.getString("username") +
                        "  password =  " + setResult.getString("password") +
                        "nickname  ="  + setResult.getString("nickname"));
            }else
            {
                System.out.println("账号或密码有误");
            }
        }catch (SQLException e)
        {
            e.printStackTrace();
        }


    }
}
